/*jslint sloppy: true */
var LM = require('../global');

LM.filter('logBody', function () {
	var templateParser = function (string, scope) {
		return string.replace(/\$\{([\s\S]*?)\}/g, function ($, code) {
			var scoped = code.replace(/(["'\.\w\$]+)/g, function (match) {
				return (/["'\d]/).test(match[0]) ? match : 'scope.' + match;
			});
			try {
				return new Function('scope', 'return ' + scoped)(scope);
			} catch (e) { return '错误：\n' + e; }
		});
	};
		
	var template = {
		0x00: "进入 ${processName} 过程",
		0x01: "退出过程",
		0x02: "${isPassed?'成功':'异常'} 退出本次循环",

		0x11: "等待 ${delay} 毫秒",
		0x12: "${action} 动作执行${isPassed?'成功':'失败'}，${param&&param.value?'参数：'+param.value+',':''} 对象:${object}",
		0x13: "断言 ${isPassed?'成功':'失败'}",
		0x14: "跳转到 ${url}",
		0x15: "刷新",
		"-1": "退出第 ${timeNumber} 次测试",
		"100": "发生错误"
	};
	
	return function (input) {
		return templateParser(template[input.type], input);
	};
});
